package com.webservice.store;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletContext;

import com.webservice.bean.QueryResult;
import com.webservice.bean.Student;
import com.webservice.bean.StudentPost;
import com.webservice.bean.Teacher;
import com.webservice.mail.MailSender;

public class DataAdapter 
{
	public Random random = new Random((new Date()).getTime());
	String strRandomTeacherID = "";
	public static String ftlfileurl;
	static MailSender mailsender=new MailSender();
	
	public static String PostedStudentID;
	public static String StudentQuestion;
	public static String StudentQuestionType;
	public static String StudentPostedTeacherID;
	
	public static String PostedTeacherID;
	
	public static String StudentName;
	public static String StudentCollege;
	public static String StudentCourse;
	
	public static String TeacherName;
	public static String TeacherCollege;
	public static String TeacherQualification;
	public static String TeacherDesignation;
	
	public static String MobileNo;
	public static String EmailID;
	public static String Password;
	
	public static String PostID;
	public static String Answer;
	public static String ViewPost;
	public static String FileContent;
	
	public static String StudentID;
	
	
	private static Map<String,Student> studentValue;
	private static Map<String,Teacher> dbQueryValue;
	private static Map<String,StudentPost> dbStudentPostValue;
	private static Map<String,QueryResult> dbQueryResult;
	private static DataAdapter instanceAdapter=null;
		
	public DataAdapter(String MobileNO,String UserType)
	{
		dbQueryValue=new HashMap<String,Teacher>();
		studentValue=new HashMap<String,Student>();
		dbStudentPostValue=new HashMap<String,StudentPost>();
		dbQueryResult=new HashMap<String,QueryResult>();
		
		if(UserType.equals("Teacher"))
		selectTeacher(MobileNO);
		else if(UserType.equals("Student"))
		selectStudent(MobileNO);	
		else if(UserType.equals("ViewStudentPostedQuery"))
		SelectStudentPost(MobileNO);
		else if(UserType.equals("AddPost"))
		addStudentPost();
		else if(UserType.equals("AddStudent"))
		addStudent();
		else if(UserType.equals("AddTeacher"))
		addTeacher();
		else if(UserType.equals("ReplyPostQuery"))
		replyPostQuery();
		else if(UserType.equals("AnswaredQuery"))
		ViewTeacherAnswer();
	}
	
	public static Map<String, QueryResult> getStudentQueriesStatus(String UserType)
	{ 		
		if(instanceAdapter!=null)
		{
			instanceAdapter = null;
		}
		if(instanceAdapter==null)
		{
			instanceAdapter =  new DataAdapter(null, UserType);
		}
		return dbQueryResult;
	}	

	public static Map<String,QueryResult> insertQuery(String UserType,ServletContext servletContext)
	{
		ftlfileurl=servletContext.getRealPath("/");
		int index=(ftlfileurl.indexOf(".")-1);
		ftlfileurl=ftlfileurl.substring(0, index);
		ftlfileurl=ftlfileurl+"\\QuerySTA\\WebContent\\WEB-INF\\ftl\\";
		
		if(instanceAdapter!=null)
		{
			instanceAdapter=null;
		}
		 if(instanceAdapter==null)
		{
			instanceAdapter=new DataAdapter(null, UserType);
		}
		return dbQueryResult;
	}
	
	public static Map<String,StudentPost> AnswaredQuery(String UserType)
	{
		if(instanceAdapter!=null)
		{
			instanceAdapter=null;
		}
		 if(instanceAdapter==null)
		{
			instanceAdapter=new DataAdapter(null, UserType);
		}
		return dbStudentPostValue;
	}
	
	
	public static Map<String,QueryResult> UpdataQueryPost(String UserType)
	{
		if(instanceAdapter!=null)
		{
			instanceAdapter=null;
		}
		 if(instanceAdapter==null)
		{
			instanceAdapter=new DataAdapter(null,UserType);
		}
		return dbQueryResult;
	}
	
	public static Map<String,QueryResult> getStudentPostStatus(String TeacherID,String UserType)
	{
		if(instanceAdapter!=null)
		{
			instanceAdapter=null;
		}
		 if(instanceAdapter==null)
		{
			instanceAdapter=new DataAdapter(TeacherID, UserType);
		}
		return dbQueryResult;
	}
	
	public static Map<String,StudentPost> getStudentPostDetail(String StudentPostID,String UserType)
	{
		if(instanceAdapter!=null)
		{
			instanceAdapter=null;
		}
		 if(instanceAdapter==null)
		{
			instanceAdapter=new DataAdapter(StudentPostID,UserType);
		}
		
		return dbStudentPostValue;		
	}
	
	public static Map<String,Teacher> getTeacherDetail(String MobileNO,String UserType)
	{
		if(instanceAdapter!=null)
		{
			instanceAdapter=null;
		}
		 if(instanceAdapter==null)
		{
			System.out.println("mobile no2"+MobileNO);
			instanceAdapter=new DataAdapter(MobileNO,UserType);
		}		
		
		return dbQueryValue;
	}
	
	public static Map<String,Student> getStudentDetail(String MobileNO,String UserType)
	{	
		System.out.println("student map");
		System.out.println("View student mobile no2"+MobileNO);
		System.out.println("View student  UserType"+UserType);
		System.out.println("instanceAdapter="+instanceAdapter);
		if(instanceAdapter!=null)
		{
			instanceAdapter=null;
		}
		
		if(instanceAdapter==null)
		{
			System.out.println("View student mobile no2"+MobileNO);
			instanceAdapter=new DataAdapter(MobileNO,UserType);
		}
		return studentValue;
	}
	
	public void addStudentPost()
	{
		  System.out.println("MySQL Connect Example.");		    
		  Connection conn = null;		  
		  Statement stmtCheck = null;		  
		  PreparedStatement pstmt = null;	
		  ResultSet rs = null;		  
		  String Status = null;
		  String Description = null;
		  
		  try 
		  {
			  conn = Dbconnector.getConnection();				  			  
		      String qry="Select Mobile_No from teacher  where Mobile_No ='"+PostedTeacherID+"' ";
		      stmtCheck = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		      rs = stmtCheck.executeQuery(qry);	
		      rs.last();
		      int i = rs.getRow();
		      rs.beforeFirst();
		      if(i==0)
		      {
		    	  	Status = "false" ;
		    	  	Description = "Teacher ID Doesn't exist";		      		
		      }	
		      else if(i>0)
		      {		    	  
		    	  String query = "insert into student_queries(Student_Id,Question,Option_Type,Teacher_Id) values(?,?,?,?)";			  
				  pstmt = conn.prepareStatement(query); 
				  pstmt.setString(1, PostedStudentID);
				  pstmt.setString(2, StudentQuestion);
				  pstmt.setString(3, StudentQuestionType);
				  pstmt.setString(4, PostedTeacherID);
				  int ii=pstmt.executeUpdate();
				  Status = "true";
				  Description="Successfully Posted";
		  	  }			 

	      	
		    QueryResult value = new QueryResult(Status,Description);
      		dbQueryResult.put("",value);
		  	
		  	if(rs!=null)
		  	{
			  	rs.close();
			}		  	
		  	if(stmtCheck!=null)
		  	{
		  		stmtCheck.close();
		  	}
		  	if(pstmt!=null)
		  	{
		  		pstmt.close();
			}
		  	if(conn!=null)
		  	{
		  		conn.close();
		  	}
		  	System.out.println("Disconnected from insert user");
		  }
		  catch (Exception e) 
		  {
			  e.printStackTrace();
		  }
	}
	
	
	public void replyPostQuery()
	{
		System.out.println("MySQL Connect Example.");
		Connection conn = null;
		Statement stmt = null;
		Statement stmtUpdate = null;
		ResultSet rs = null;
		String Status = null;
		String Description = null;
		  
		try 
		{
			  conn = Dbconnector.getConnection();	  
			  stmtUpdate = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
			  String selectQuesry="Select Student_Queries_ID from student_queries  " +" where Student_Queries_ID ='"+PostID+"' ";
				  
			  rs = stmtUpdate.executeQuery(selectQuesry);
			  rs.last();
			  int k = rs.getRow();
			  rs.beforeFirst();
			  if(k > 0)
			  {
				  
				  stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
				  int rows = stmt.executeUpdate( "Update student_queries set Answer = '"+Answer+"', ViewPost='"+ViewPost+"', File_Content='"+FileContent+"' "+                         
				  " where Student_Queries_ID ='"+PostID+"'  " ) ;

				  if(rows > 0)
				  {	 
					  Status = "true";
					  Description="Query Answer Posted.!";
				  }
			  }
			  else
			  {
				  Status = "false";
				  Description="Query Not Exist";
			
			  }

			  QueryResult value = new QueryResult(Status,Description);
			  dbQueryResult.put("",value);
			
	      	  if(rs!=null)
	      	  {
	      		  rs.close();
	      	  }
	      	  if(stmt!=null)
	      	  {
	      		  stmt.close();
	      	  }	      	      	 
	      	  if(conn!=null)
	      	  {
	      		  conn.close();
	      	  }
	      	  System.out.println("Disconnected from update post details");
		}
		catch (Exception e)
		{
			  e.printStackTrace();
		}
	}
	
	public void SelectStudentPost(String Teacher_ID)
	{
		
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;			  
		StudentPost qb = null;
		String Status = null;
		
		ArrayList<StudentPost> Listbean = new ArrayList<StudentPost>();
		try 
		{
			  conn = Dbconnector.getConnection();
			  
			  String query = "select a.Student_Queries_ID, a.student_id,a.Teacher_Id, a.question, b.countt from student_queries a join(select student_id, count(student_id) countt from student_queries where teacher_id = '"+Teacher_ID+"' group by student_id)b on a.student_id = b.student_id where a.teacher_id = '" +Teacher_ID+"'";
		      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		      rs = stmt.executeQuery(query);
		      rs.last();
		      int NoOfRows = rs.getRow();
		      System.out.println("rowCount=" + NoOfRows);
		      rs.beforeFirst();
		      
		      if(NoOfRows>0)
		      {
		    	  
		    	  while(rs.next())
		    	  {
		    		  	qb = new StudentPost();
		      			if(rs.getString("Student_Queries_ID")!=null)
			  			{
			  				qb.setPostID(rs.getString("Student_Queries_ID"));
			  			}			  			  			
			  			if(rs.getString("Question")!=null)
			  			{
			  				qb.setQuestion(rs.getString("Question"));
			  			}	 
			  			if(rs.getString("Teacher_Id")!=null)
			  			{
			  				qb.setTeacherID(rs.getString("Teacher_Id"));
			  			}
			  			
			  			if(rs.getString("student_id")!=null)
			  			{
			  				qb.setStudentID(rs.getString("student_id"));
			  			}
			  			
			  			if(rs.getString("countt")!=null)
			  			{
			  				qb.setVisitedCount(rs.getString("countt"));
			  			}
			  			Status="true";
			  			Listbean.add(qb);			  
		  			}			      					      		
		      }
		      else
		      {
		    	  	qb = new StudentPost();
		    	  	Status="false";
		    	    qb.setErrorDesc("No Queries to this Teacher");
		    	    Listbean.add(qb);			  
		      }	      
		      
			      
			  System.out.println("List Size "+Listbean.size());
			  if(Listbean!=null )
			  {	  	
				  	StudentPost value = new StudentPost(Listbean,Status,NoOfRows);
				  	dbStudentPostValue.put("0",value);			  		
			  }	
	
			  if(rs!=null)
			  {
			  		rs.close();
			  }
			  if(stmt!=null)
			  {
			  		stmt.close();
			  }				  	
			  if(conn!=null)
			  {
			  		conn.close();
			  }
			  System.out.println("Disconnected from database");
		}
		catch(Exception e)
		{
			System.out.println("expo="+e.getLocalizedMessage());			
		}

		
	}
	
	public void selectStudent(String MobileNO)
	{	
			
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;			  
		Student qb = null;
		ArrayList<Student> Listbean = new ArrayList<Student>();
		try 
		{
			  conn = Dbconnector.getConnection();
			  System.out.println("Select student() begin");
			  System.out.println("select student="+MobileNO);
			  String query = "SELECT Student_ID,Student_Name, College_Name, Course,Email_ID,Password FROM student where Mobile_No='"+MobileNO+"'";
		      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		      rs = stmt.executeQuery(query);
		      rs.last();
		      int i = rs.getRow();
		      System.out.println("rowCount=" + i);
		      rs.beforeFirst();
		      qb = new Student();
		      if(i>0)
		      {
		    	  qb.setStatus("true");
		    	  while(rs.next())
		    	  {		      					      					      			
		      			if(rs.getString("Student_ID")!=null)
			  			{
			  				qb.setStudentID(rs.getString("Student_ID"));
			  			}	
			  			if(rs.getString("Student_Name")!=null)
			  			{
			  				qb.setStudentName(rs.getString("Student_Name"));
			  			}			  			  			
			  			if(rs.getString("College_Name")!=null)
			  			{
			  				qb.setCollageName(rs.getString("College_Name"));
			  			}	 
			  			if(rs.getString("Course")!=null)
			  			{
			  				qb.setCourse(rs.getString("Course"));
			  			}
			  			
			  			if(rs.getString("Email_ID")!=null)
			  			{
			  				qb.setEmailID(rs.getString("Email_ID"));
			  			}
			  			
			  			if(rs.getString("Password")!=null)
			  			{
			  				qb.setPassword(rs.getString("Password"));
			  			}
			  			
		  			}			      					      		
		      }
		      else
		      {
		    	    qb.setStatus("false");
		    	    qb.setErrorDesc("Student not register with us");
		      }	      
		      
			  Listbean.add(qb);			      
			  System.out.println("List Size "+Listbean.size());
			  if(Listbean!=null )
			  {	  	
				  	Student value = new Student(Listbean);
				  	studentValue.put("0",value);			  		
			  }	
			  i=0;
			  if(rs!=null)
			  {
			  		rs.close();
			  }
			  if(stmt!=null)
			  {
			  		stmt.close();
			  }				  	
			  if(conn!=null)
			  {
			  		conn.close();
			  }
			  System.out.println("Select student() end db");
			  
		}
		catch(Exception e)
		{
			System.out.println("expo="+e.getLocalizedMessage());			
		}
		
	}
	
	public void selectTeacher(String MobileNO)
	{
			Connection conn = null;
			Statement stmt = null;
			ResultSet rs = null;			  
			Teacher qb = null;
			ArrayList<Teacher> Listbean = new ArrayList<Teacher>();
			try 
			{
				  conn = Dbconnector.getConnection();
				  String query = "SELECT * FROM teacher where Mobile_No='"+MobileNO+"'";
			      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
			      rs = stmt.executeQuery(query);
			      rs.last();
			      int i = rs.getRow();
			      System.out.println("rowCount=" + i);
			      rs.beforeFirst();
			      qb = new Teacher();
			      if(i>0)
			      {
			    	  qb.setStatus("true");
			    	  while(rs.next())
			    	  {		      					      					      			
			      			if(rs.getString("Teacher_Id")!=null)
				  			{
				  				qb.setTeacherID(rs.getString("Teacher_Id"));
				  			}	
				  			if(rs.getString("Teacher_Name")!=null)
				  			{
				  				qb.setTeacherName(rs.getString("Teacher_Name"));
				  			}			  			  			
				  			if(rs.getString("College_Name")!=null)
				  			{
				  				qb.setCollege_Name(rs.getString("College_Name"));
				  			}	 
				  			if(rs.getString("Qualification")!=null)
				  			{
				  				qb.setQualification(rs.getString("Qualification"));
				  			}
				  			if(rs.getString("Designation")!=null)
				  			{
				  				qb.setDesignation(rs.getString("Designation"));
				  			}
				  			if(rs.getString("Mobile_No")!=null)
				  			{
				  				qb.setMobileNo(rs.getString("Mobile_No"));
				  			}	 
				  			if(rs.getString("Email_ID")!=null)
				  			{
				  				qb.setEmailID(rs.getString("Email_ID"));
				  			}
				  			if(rs.getString("Password")!=null)
				  			{
				  				qb.setPassword(rs.getString("Password"));
				  			}
			  			}			      					      		
			      }
			      else
			      {
			    	    qb.setStatus("false");
			    	    qb.setErrorDesc("Teacher not register with us");
			      }	      
			      
				  Listbean.add(qb);			      
				  System.out.println("List Size "+Listbean.size());
				  if(Listbean!=null )
				  {	  	
					  	Teacher value = new Teacher(Listbean);
				  		dbQueryValue.put("0",value);			  		
				  }	
		
				  if(rs!=null)
				  {
				  		rs.close();
				  }
				  if(stmt!=null)
				  {
				  		stmt.close();
				  }				  	
				  if(conn!=null)
				  {
				  		conn.close();
				  }
				  System.out.println("Disconnected from database");		
			}
			catch(Exception e)
			{
				System.out.println("expo="+e.getLocalizedMessage());
			}
	}
	
	public void addStudent()
	{
		  System.out.println("MySQL Connect addStudent.");		    
		  Connection conn = null;		  
		  Statement stmtCheck = null;		  
		  PreparedStatement pstmt = null;	
		  ResultSet rs = null;		  
		  String Status = null;
		  String Description = null;
		  
		  try 
		  {
			  conn = Dbconnector.getConnection();				  			  
			  System.out.println("addstudent mobileNo="+MobileNo);
		      String qry="Select * from student  where Mobile_No ='"+MobileNo+"' ";
		      stmtCheck = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		      rs = stmtCheck.executeQuery(qry);	
		      rs.last();
		      int i = rs.getRow();
		      System.out.println("Row count="+i);
		      rs.beforeFirst();
		      if(i==0)
		      {		    	  		      		
		    	  String query = "insert into student(Student_Name,College_Name,Course,Mobile_No,Email_ID,Password) values(?,?,?,?,?,?)";			  
				  pstmt = conn.prepareStatement(query); 
				  pstmt.setString(1,StudentName);
				  pstmt.setString(2,StudentCollege);
				  pstmt.setString(3,StudentCourse);
				  pstmt.setString(4,MobileNo);
				  pstmt.setString(5,EmailID);
				  pstmt.setString(6,Password);
				  
				  int ii=pstmt.executeUpdate();
				  Status = "true";
				  Description="Successfully Registered";

		      }	
		      else if(i>0)
		      {		    	  		    	  
		    	  Status = "false" ;
		    	  Description = "Mobile No Already Exist.";			    	  	
		  	  }			 

	      	
		    QueryResult value = new QueryResult(Status,Description);
      		dbQueryResult.put("",value);
		  	i=0;
		  	if(rs!=null)
		  	{
			  	rs.close();
			}		  	
		  	if(stmtCheck!=null)
		  	{
		  		stmtCheck.close();
		  	}
		  	if(pstmt!=null)
		  	{
		  		pstmt.close();
			}
		  	if(conn!=null)
		  	{
		  		conn.close();
		  	}
		  	System.out.println("Disconnected from insert addStudent");
		  }
		  catch (Exception e) 
		  {
			  e.printStackTrace();
		  }

	}
	
	public void addTeacher()
	{
		  System.out.println("MySQL Connect Example.");		    
		  Connection conn = null;		  
		  Statement stmtCheck = null;		  
		  PreparedStatement pstmt = null;	
		  ResultSet rs = null;		  
		  String Status = null;
		  String Description = null;
		  String TeacherSessionID = null;
		  
		  try 
		  {
			  conn = Dbconnector.getConnection();
//			  MobileNo=generateRandomString();
		      String qry="Select * from teacher  where Mobile_No ='"+MobileNo+"' ";
		      stmtCheck = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		      rs = stmtCheck.executeQuery(qry);	
		      rs.last();
		      int i = rs.getRow();
		      rs.beforeFirst();
		      if(i==0)
		      {		    	  		      		
		    	  String query = "insert into teacher(Teacher_Name,College_Name,Designation,Mobile_No,Email_ID,Password) values(?,?,?,?,?,?)";			  
				  pstmt = conn.prepareStatement(query); 
				  pstmt.setString(1,TeacherName);
				  pstmt.setString(2,TeacherCollege);
//				  pstmt.setString(3,TeacherQualification);
				  pstmt.setString(3,TeacherDesignation);
				  pstmt.setString(4,MobileNo);
				  pstmt.setString(5,EmailID);
				  pstmt.setString(6,Password);
				  
				  int ii=pstmt.executeUpdate();
				  Status = "true";
				  Description="Successfully Registered";
				  TeacherSessionID=MobileNo;
				  mailsender.doMaiilProcessforRegisteredUser(TeacherName, TeacherSessionID, Password, TeacherDesignation, EmailID, ftlfileurl, "regwelcome.ftl");
		      }	
		      else if(i>0)
		      {		    	  		    	  
		    	  Status = "false" ;
		    	  Description = "Teacher ID Already Exist.";	
//		    	  addTeacher();
		  	  }			 

	      	
		    QueryResult value = new QueryResult(Status,Description,TeacherSessionID);
      		dbQueryResult.put("",value);
		  	
		  	if(rs!=null)
		  	{
			  	rs.close();
			}		  	
		  	if(stmtCheck!=null)
		  	{
		  		stmtCheck.close();
		  	}
		  	if(pstmt!=null)
		  	{
		  		pstmt.close();
			}
		  	if(conn!=null)
		  	{
		  		conn.close();
		  	}
		  	System.out.println("Disconnected from insert user");
		  }
		  catch (Exception e) 
		  {
			  e.printStackTrace();
		  }
	}
	
	public void ViewTeacherAnswer()
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;			  
		StudentPost qb = null;
		String Status = null;
		
		ArrayList<StudentPost> Listbean = new ArrayList<StudentPost>();
		try 
		{
			  conn = Dbconnector.getConnection();
			  
			  String query = "SELECT Student_Queries_ID,Student_Id, Question,Teacher_Id ,Answer,ViewPost,File_Content FROM student_queries where Student_Id="+StudentID +" and Answer!='' and Answer is not null or ViewPost='1' order by ViewPost";
		      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		      rs = stmt.executeQuery(query);
		      rs.last();
		      int NoOfRows = rs.getRow();
		      System.out.println("StudentID="+StudentID);
		      System.out.println("rowCount=" + NoOfRows);
		      rs.beforeFirst();
		      
		      if(NoOfRows>0)
		      {		    	  
		    	  while(rs.next())
		    	  {
		    		  	qb = new StudentPost();

		    		  	if(rs.getString("Answer")!=null)
			  			{
			  				qb.setAnswer(rs.getString("Answer"));
			  			}
		    		  	
		    		  	if(rs.getString("Student_Queries_ID")!=null)
			  			{
			  				qb.setPostID(rs.getString("Student_Queries_ID"));
			  			}			  			  			
			  			if(rs.getString("Question")!=null)
			  			{
			  				qb.setQuestion(rs.getString("Question"));
			  			}	 
			  			if(rs.getString("Teacher_Id")!=null)
			  			{
			  				qb.setTeacherID(rs.getString("Teacher_Id"));
			  			}
			  			if(rs.getString("ViewPost")!=null)
			  			{
			  				qb.setViewPost(rs.getString("ViewPost"));
			  			}
			  			if (rs.getString("File_content")!=null)
			  			{
							qb.setFileContent(rs.getString("File_content"));
						}
			  			else 
			  			{
			  				qb.setFileContent("null");
			  			}
			  			
			  			Status="true";			  			
			  			Listbean.add(qb);			  
		  			}			      					      		
		      }
		      else
		      {
		    	  	qb = new StudentPost();
		    	  	Status="false";
		    	    qb.setErrorDesc("Answer Not Found");
		    	    Listbean.add(qb);			  
		      }	      
		      
			      
			  System.out.println("List Size "+Listbean.size());
			  if(Listbean!=null )
			  {	  	
				  	StudentPost value = new StudentPost(Listbean,Status,NoOfRows);
				  	dbStudentPostValue.put("0",value);			  		
			  }	
	
			  if(rs!=null)
			  {
			  		rs.close();
			  }
			  if(stmt!=null)
			  {
			  		stmt.close();
			  }				  	
			  if(conn!=null)
			  {
			  		conn.close();
			  }
			  System.out.println("Disconnected from database");
		}
		catch(Exception e)
		{
			System.out.println("expo="+e.getLocalizedMessage());			
		}

	}	

    public String generateRandomString()
    {
//      char[] values = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t',
//               			'u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9'};

    	char[] values = {'0','1','2','3','4','5','6','7','8','9'};

    	for (int i=0;i<4;i++) 
    	{
    		int idx=random.nextInt(values.length);
    		strRandomTeacherID += values[idx];
    	}
    	System.out.println("Random TeacherID="+strRandomTeacherID);
    	return strRandomTeacherID;
    }

}
